/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.drawee;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.ShowcaseApplication;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.oszc.bbhmlibrary.utils.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.utils.net.Uri;

public class DraweeSimpleAbility extends Ability {
    public static String TAG = DraweeSimpleAbility.class.getSimpleName();
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(com.facebook.fresco.samples.showcase.ResourceTable.Layout_layout_drawee_simple);
        final SimpleDraweeView simpleDraweeView = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view);
     //   Uri progressUri = Uri.parse("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F8%2F58cceb22545f4.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623295237&t=28feb790c6eee323b4e515e426682238");
     //   simpleDraweeView.setImageURI(progressUri);
        /*
        RoundingParams params = RoundingParams.fromCornersRadii(20,20,20,20);
        params.setPaintFilterBitmap(true);
        simpleDraweeView.getHierarchy().setRoundingParams(params);
         */

        /*
        RoundingParams roundparams = RoundingParams.fromCornersRadius(120);
        //roundparams.setRoundAsCircle(true);
        roundparams.setRoundingMethod(RoundingParams.RoundingMethod.BITMAP_ONLY);
        roundparams.setPaintFilterBitmap(false);
        //roundparams.setBorder(Color.RED.getValue(), 200);
        simpleDraweeView.getHierarchy().setRoundingParams(roundparams);
        simpleDraweeView.setActualImageResource(ResourceTable.Media_blue);
         */

        /*
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(progressUri)
                .setProgressiveRenderingEnabled(true)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(simpleDraweeView.getController())
                .build();
        simpleDraweeView.setController(controller);

         */


        final Component component = findComponentById(ResourceTable.Id_btn_random_uri);
        final Uri uri = ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.M,null,null);
        simpleDraweeView.setImageURI(uri);
        component.setClickedListener(view -> {
            Uri newImageUri= ShowcaseApplication.imageUriProvider.createSampleUri(ImageUriProvider.ImageSize.M,null,null);
            LogUtil.error(TAG,newImageUri.toString());
            simpleDraweeView.setImageURI(newImageUri);
        });
    }
}
